package ute.mvc.controller.home;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ute.mvc.model.Course;
import ute.mvc.model.OpenCourse;
import ute.mvc.model.Subject;
import ute.mvc.model.User;
import ute.mvc.model.User.GroupLogin;
import ute.mvc.service.CourseService;
import ute.mvc.service.OpenCourseService;
import ute.mvc.service.UserService;
import ute.mvc.util.PasswordUtil;

@Controller
@RequestMapping(value = { "/", "/home" })
public class LoginController {

	@Autowired
	UserService userService;

	@Autowired
	CourseService courseService;

	@Autowired
	OpenCourseService openCourseService;

	@RequestMapping(method = RequestMethod.GET)
	public String getLogin(HttpServletRequest request, Model model) {

		User logedUser = (User) request.getSession().getAttribute("user");
		if(logedUser != null){
			if(logedUser.getRole() == 1){
				//Admin
				return "redirect:portal";
			}else if(logedUser.getRole() == 2){
				//Lecturer
				return "redirect:lecturer";
			}else{
				//Student
				return "redirect:student";
			}
		}
		
		model.addAttribute("user", new User());
		return "home";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String setLogin(
			@Validated(GroupLogin.class) @ModelAttribute User user,
			BindingResult result, ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		if (result.hasErrors())
			return "home";

		User user1 = userService.getUserByEmail(user.getEmail());
		if (user1 != null
				&& PasswordUtil.hashPassword(user.getPassword()).equals(
						user1.getPassword())) {
			request.getSession().setAttribute("user", user1);
			if (user1.getRole() == 1) {

				List<Course> courses = courseService.getAllCourse();
				request.getSession().setAttribute("courses", courses);
				return "redirect:portal";
			} else if (user1.getRole() == 2) {
				List<OpenCourse> courses = userService
						.getListOpenCourseWithLecturerId(user1.getUserId());
				// Get Subject for each oCourse
				for (OpenCourse o : courses) {
					List<Subject> lstSubject = openCourseService
							.getSubjectByOpenCourseId(o.getOpenCourseId());
					o.setListSubject(lstSubject);
				}
				request.getSession().setAttribute("courses", courses);
				return "redirect:lecturer";
			} else{
				List<OpenCourse> courses = openCourseService.getOpenCourseByStudent(user1);
				System.out.println("Student ");
				System.out.println(courses.size());
				// Get Subject for each oCourse
				for (OpenCourse o : courses) {
					List<Subject> lstSubject = openCourseService
							.getSubjectByOpenCourseId(o.getOpenCourseId());
					o.setListSubject(lstSubject);
				}
				request.getSession().setAttribute("courses", courses);
				return "redirect:student";
			}
		} else {
			result.rejectValue("password", "loginFail");
			return "home";
		}
	}

	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {

		request.getSession().invalidate();
		return "redirect:/";
	}
	
	@RequestMapping("/deny")
	public String deny() {

		return "access_denied";
	}
}
